$(function () {
		    $("#btnAdd").click(function () { addRow(); }).linkbutton({plain: true });
		    $("#productAdd").datagrid({
		        columns: [[
		            { field: "Name", title: "名称", width: 100 ,align:"center"},
		            {
		                field: "Class", title: "类别", width: 100,align:"center",
		                editor: {
		                    type: "text",
		                    options: {
		                        required: true
		                    }
		                }
		            },
		            {
		                field: "Number", title: "数量", width: 80,align:"center",
		                editor: {
		                    type: "text",
		                    options: {
		                        required: true
		                    }
		                }
		            },
		             {
		                field: "Price", title: "单价", width: 80,align:"center",
		                editor: {
		                    type: "text",
		                    options: {
		                        required: true
		                    }
		                }
		            },
		             {
		                field: "Total", title: "总价", width: 80,align:"center",
		                editor: {
		                    type: "text",
		                    options: {
		                        required: true
		                    }
		                }
		            },
		            {
		                field: "Operate", title: "操作", width: 80,align:"center",
		                editor: {
		                    type: "text",
		                    options: {
		                        required: true
		                    }
		                }
		            }
		        ]],
		        fitColumns: true,
		        height: 250,
		        width:670,
		        onClickRow: onClickRow
		    });
		});
		// 判断时候存在编辑中的行
		var editIndex = null;
		function endEditing() {
		    if (editIndex == null) { return true }
		    if ($('#productAdd').datagrid('validateRow', editIndex)) {
		        $('#productAdd').datagrid('endEdit', editIndex);
		        editIndex = null;
		        return true;
		    } else {
		        return false;
		    }
		}
		// datagrid行点击事件
		function onClickRow(index, row) {
		    if (editIndex != index) {
		        if (endEditing()) {
		            $("#productAdd").datagrid("selectRow", index).datagrid("beginEdit", index);
		            editIndex = index;
		        } else {
		            $("#productAdd").datagrid("selectRow", editIndex);
		        }
		    }
		}
		// 添加一行
		function addRow() {
		    if (endEditing()) {
		        //$("#dg").datagrid("appendRow");
		        var Pname=document.getElementById("Pname").value;
				var Pclass=document.getElementById("Pclass").value;
				var Pnumber=document.getElementById("Pnumber").value;
				var Pprice=document.getElementById("Pprice").value;
				var Ptotal=parseFloat(Pnumber)*parseFloat(Pprice);
		        $("#productAdd").datagrid("appendRow", {
		            Name: Pname,
		            Class: Pclass,
		            Number: Pnumber,
		            Price:Pprice,
		            Total:Ptotal,
		            Operate:"删除"
		        });
		    }
		}
		/*点击计算按钮时,出现结算内容*/
		function pay(){
			var payResult=document.getElementById("payResult");
			payResult.innerHTML="总金额：￥123456";
		}	